import time
from array import array
from datetime import datetime
from fileinput import filename
from lib2to3.pgen2.grammar import opmap_raw
from time import strftime
from unittest import expectedFailure

from encodings.uu_codec import uu_encode
from webbrowser import Chrome

from idna import ulabel
from requests import options
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService, Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v127.css import CSSLayer
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import element_selection_state_to_be
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

# #驱动程序管理的⾃动化
# #创建驱动对象
# #设置安装驱动管理器
# chromeins = ChromeDriverManager().install()
# #创建谷歌驱动对象
# driver = webdriver.Chrome(service=Service(chromeins))
# # driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# #2.输⼊百度⽹址:https://www.baidu.com
# driver.get("https://www.baidu.com")
# time.sleep(2)
# #3、找到输⼊框并输⼊“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# time.sleep(2)
# #4、找到“百度⼀下”按钮并点击
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# # time.sleep(2)
# #5、关闭浏览器
# driver.quit()

#设置安装驱动管理器
# chromeins = ChromeDriverManager().install()
# #创建谷歌驱动对象
# driver = webdriver.Chrome(service=Service(chromeins))
# driver.get("https://www.baidu.com")
#打开b站
# driver.get("https://www.bilibili.com/")
# # time.sleep(5)
# # ret = driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(2)")
# # ret = driver.find_elements(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li")
# # time.sleep(5)
#
# # for i in ret:
# #     print(i.text)
# #首先使用标签选择器
# driver.find_element(By.CSS_SELECTOR,"#i_cecream > div.bili-feed4 > div.bili-header.large-header > div.bili-header__bar > ul.left-entry > li:nth-child(2) > a > span").click()
# time.sleep(3)
# driver.quit()

##################################################################################
##登录比特教务管理系统
#安装测试驱动管理器
options = webdriver.ChromeOptions()
# options.add_argument("-haedless")
# options.page_load_strategy = "normal"
# options.page_load_strategy = "eager"
options.page_load_strategy = "none"
ChromeIn = ChromeDriverManager().install()
#创建驱动对象
driver = webdriver.Chrome(service=Service(ChromeIn),options=options)
driver.get("https://www.baidu.com")
# time.sleep(2)
driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(5)").click()
print(driver.title)
#访问比特教务系统
# driver.get("https://edu.bitejiuyeke.com/login")
# driver.get("https://www.baidu.com")
#寻找输入框进行输入
# driver.find_element(By.CSS_SELECTOR,"#app > div.login > form > div.el-form-item.is-error.is-required.el-form-item--medium > div > div.f12-placeholder.el-input.el-input--medium.el-input--prefix > input").send_keys("17537647903")
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,"#app > div.login > form > div:nth-child(3) > div > div > input").send_keys("zhanghao1")
# time.sleep(3)
# driver.find_element(By.CSS_SELECTOR,"#app > div.login > form > div:nth-child(4) > div > button").click()
# time.sleep(3)
# driver.quit()

# text = driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(1) > a > span.title-content-title").text
# title = driver.title
# url = driver.current_url
# # assert url == title
# assert url == url
# print(url)
# print(title)
# print(text)
# print("befor"+driver.title)
# print("befor"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > a:nth-child(6)").click()
# #获取页面的所以的句柄
# handle = driver.window_handles
# #获取当前啊页面的句柄
# currenthandle = driver.current_window_handle
# for hand in handle:
#     if hand != currenthandle:
#         currenthandle = hand
# #切换句柄
# driver.switch_to.window(currenthandle)
#
# print("after"+driver.title)
# print("after"+driver.current_url)

#######################################################
#设置窗口的大小
# driver.maximize_window()
# #全屏
# driver.fullscreen_window()
# # time.sleep(2)
# # driver.minimize_window()
# # time.sleep(2)
# # driver.set_window_size(100,200)
# # time.sleep(2)
# #保存屏幕截图
# driver.save_screenshot("text.png")

#将文件保存到一个文件夹中
# filename = "autotest-"+datetime.now().strftime('%Y-%m-%d-%H%M%S')+'.png'
# driver.save_screenshot('./images/'+filename)

############################################################################
#窗口中的弹窗

# driver.get("file:///C:/Users/34170/Desktop/selenium-html/selenium-html/alert.html#")
# driver.find_element(By.CSS_SELECTOR,"#tooltip").click()
# #将驱动切换到弹窗中
# arlt = driver.switch_to.alert
# time.sleep(2)
# #同意
# arlt.accept()
# time.sleep(2)

# driver.get("file:///C:/Users/34170/Desktop/selenium-html/selenium-html/confirm.html")
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# time.sleep(2)
# arlt = driver.switch_to.alert
# arlt.accept()
# # arlt.dismiss()
# time.sleep(2)
# driver.quit()

# driver.get("file:///C:/Users/34170/Desktop/selenium-html/selenium-html/Prompt.html")
# driver.find_element(By.CSS_SELECTOR,"body > input[type=button]").click()
# alrt = driver.switch_to.alert
# time.sleep(2)
# alrt.send_keys("遇事不决可问春风！")
# time.sleep(2)
# alrt.accept()
# time.sleep(2)

#隐式等待
# driver.implicitly_wait(3)
# driver.get("https://www.baidu.com")
#
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("剑来")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# # driver.find_element(By.CSS_SELECTOR,"#\\31  > div > div.c-row > div.c-span9.bookifffffffffnfo-intro_2snZH.c-span-last > a > button").click()
# #添加显示等待
# # wait = WebDriverWait(driver,3)
# # ret = invisibility_of_element((By.CSS_SELECTOR,"#\\31  > div > div:nth-child(3) > div:nth-child(1) > div.chapter-content_2HmnG > div > a"))
# # wait.until(ret)
# wait = WebDriverWait(driver,3)
# wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,"#\\31  > div > div.c-row.bookinfo_1k-rc > div.c-span9.bookinfo-intro_2snZH.c-span-last > a > button")))
# # driver.find_element(By.CSS_SELECTOR,"#\\31  > div > div.c-row.bookinfo_1k-rc > div.c-span9.bookinfo-intro_2snZH.c-span-last > a > button").click()
# # time.sleep(2)

##########################################################################################
#浏览器的导航
# driver.get("https://www.baidu.com")
#
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("剑来")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
#
# #界面刷新
# driver.refresh()
# time.sleep(2)
#
# #页面后退
# driver.back()
# time.sleep(2)
#
# #界面的前进
# driver.forward()
# time.sleep(2)

##################################################
#对弹出的窗口进行文件的上传
# driver.get("file:///C:/Users/34170/Desktop/selenium-html/selenium-html/upload.html")
# ele = driver.find_element(By.CSS_SELECTOR,"body > div > div > input[type=file]")
# ele.send_keys("D:\\confim.txt")
# time.sleep(2)


############################################################
#页面的加载方式无头模式访问



driver.quit()
